// pages/reviewRecord/lwordWrong/lwordWrong.js
const app = getApp();
let tool = require('../../../utils/tool.js');
const httpUtils = require('../../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    innerAudioContext: new Object(),
    wordList: {},
    wordCss: {},
    wordLen: 0,
    studyLen: 0,
    rtype: 3,
    saveName: 'lword3',
    task_id:"",
    sort:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this;
    if(options){
      that.setData({
        task_id:options.task_id,
        sort:options.sort
      })
    }
     //音频插件处理
     this.data.innerAudioContext = wx.createInnerAudioContext()
     this.data.innerAudioContext.autoplay = false;
     this.data.innerAudioContext.loop = false;
     this.data.innerAudioContext.src = app.globalData.mp3.selected;

    this.getData();
  },

  // 获取错词数据
  getData(){
    var that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getErrorWord({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      calendar_id: that.data.task_id,
    }).then((res) => {
      wx.hideLoading();
      let tmparr=res.data;
      for (let i = 0; i < (res.data).length; i++) {
        tmparr[i].dis = 'none';
        tmparr[i].click = 0;
        tmparr[i].fontw = 'normal'
      }
      that.setData({
        wordList: tmparr,
        wordLen: tmparr.length,
      });  
 
    });
  },


  //点击单词
  clickWorld: function(e) {
    let tmp = new Object;
    tmp = this.data.wordList;
    let hasClick = this.data.studyLen;
    let idx = e.currentTarget.dataset.idx;
    this.data.innerAudioContext.src = tmp[idx].vedio_url;
    this.data.innerAudioContext.play();
    if (tmp[idx].click == 0) {
      tmp[idx].fontw = 800;
    } else if (tmp[idx].click == 1) {
      tmp[idx].bgcolor = "#D6FFE7";
      tmp[idx].dis = 'flex';
      hasClick++;
    } else if (tmp[idx].click == 2) {

      if (tmp[idx].dis == 'none') {
        tmp[idx].dis = 'flex';
      } else {
        tmp[idx].dis = 'none';
      }

    }
    if (tmp[idx].click < 2) {
      tmp[idx].click = parseInt(tmp[idx].click) + 1;
    }
    this.setData({
      wordList: tmp,
      studyLen: hasClick
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})